//  this library.
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/ienum/subject/chapter_icon_enum.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';

import 'package:hktk_app/widget/subject/chapter_show_qnum_widget.dart';
import 'package:hktk_app/widget/subject/video_show_percent.dart';

/// 自定义方法
typedef ContextFun = void Function(BuildContext context);

/// 章节，最里层显示
class ChapterShowWidget extends StatefulWidget {
  /// 标题
  final String title;

  /// 右边标签具体显示
  final ChapterIconEnum iconEnum;

  /// 直接显示，不是在可展开组件里面显示，true：直接显示，false：在展开组件里面
  final bool directShow;

  /// 点击方法
  final ContextFun onTapFun;

  /// 总数
  final int total;

  /// 已做数量
  final int num;

  /// 进度条显示试题还是百分比，true：试题数，false：百分比
  final bool showQuestion;
  final bool isFinished;

  const ChapterShowWidget({
    super.key,
    required this.title,
    required this.iconEnum,
    required this.directShow,
    required this.onTapFun,
    required this.total,
    required this.num,
    required this.showQuestion,
    this.isFinished = false,
  });


  //
  // @override
  // Widget build(BuildContext context) {
  //
  //   return GestureDetector(
  //     onTap: () {
  //       onTapFun(context);
  //     },
  //     child: _contentItem(),
  //   );
  // }



  @override
  State<StatefulWidget> createState()=> _ChapterShowWidgetState();
}

class _ChapterShowWidgetState extends State<ChapterShowWidget>{
  @override
  void initState() {
    //   initState
    super.initState();


  }
  @override
  Widget build(BuildContext context) {

    return GestureDetector(
      onTap: () {
        widget.onTapFun(context);
      },
      child: _contentItem(),
    );
  }
  /// 左边内容显示
  _contentItem() {
    return Container(
      padding: EdgeInsets.only(bottom: widget.directShow ? 10 : 0),
      decoration: BoxDecoration(
        border: widget.directShow
            ? Border(bottom: BorderSide(color: colorBlack5, width: 1))
            : null,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _subItemTitle(),
          hiSpace(height: 5),
          // Text("111111"),
          _bottomItem(),
        ],
      ),
    );
  }

  /// 显示子组件顶部title
  _subItemTitle() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        if (widget.directShow) _subheadingIcon(),
        if (widget.directShow) hiSpace(width: 16),
        Expanded(child: _subheadingText()),
        _subheadingSuffixIcon(),
      ],
    );
  }

  /// 子标题图标
  _subheadingIcon() {
    return Container(
      width: 10,
      height: 10,
      decoration: BoxDecoration(
        color: colorBlue1,
        borderRadius: BorderRadius.circular(5),
      ),
    );
  }

  /// 标题内容显示
  _subheadingText() {
    return Text(
      widget.title,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: FontUtil.fontGrey2(16),
    );
  }

  /// 显示子标题的后缀
  _subheadingSuffixIcon() {
    if (widget.iconEnum == ChapterIconEnum.chapter) {
      return _iconShow(
        text: "练习",
        color: colorBlue1,
        backgroundColor: colorBlue2,
      );
    } else if (widget.iconEnum == ChapterIconEnum.course) {
      return _iconShow(text: "观看",onClick:(){
        // Navigator.pushNamed(context, "/course-detail",arguments: {
        //     "infoModel": infoModel,
        //     "chapterId": chapterId,
        //     "chapterName": chapterName,
        //     "columnId": columnId,
        //     "free": free,
        // });
        // Navigator.pushNamed(context, "/course_video",arguments: {
        //   "aliyunVideoId":
        // });
      });
    } else {
      // 未购买，显示锁图标
      return SvgPicture.asset(
        "images/home/course_s.svg",
        width: 48,
        height: 48,
      );
    }
  }

  /// 图标显示
  _iconShow({required String text, Color? color, Color? backgroundColor,Function? onClick}) {
    /*因为竞争手势问题，这里去掉手势*/

    return Container(
      width: 88,
      // height: 48,
      padding: EdgeInsets.symmetric(vertical: 3),
      decoration: BoxDecoration(
        color: backgroundColor ?? colorPink1,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: Center(
        child: Text(
          text,
          style: TextStyle(
            fontSize: 16,
            color: color ?? colorRed2,
          ),
          textAlign: TextAlign.center,
        ),
      ),
    );

    return GestureDetector(onTap: (){
      if(onClick!=null){
        onClick();
      }


    },child: Container(
      width: 88,
      // height: 48,
      padding: EdgeInsets.symmetric(vertical: 3),
      decoration: BoxDecoration(
        color: backgroundColor ?? colorPink1,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: Center(
        child: Text(
          text,
          style: TextStyle(
            fontSize: 16,
            color: color ?? colorRed2,
          ),
          textAlign: TextAlign.center,
        ),
      ),
    ));
  }

  /// 底部试题已做数量显示
  _bottomItem() {
    Widget widget1;
    if(widget.isFinished){
      widget1=VideoShowPercentWidget(
        qCount: widget.total,
        num: widget.num,
        showQuestion: widget.showQuestion,
        isFinished: true,
      );
    }else{
      widget1=ChapterShowQnumWidget(
        qCount: widget.total,
        num: widget.num,
        showQuestion: widget.showQuestion,
      );
    }
    return Padding(
      padding: EdgeInsets.only(
        left: widget.directShow ? 26 : 0,
      ),
      child: widget1,
    );
  }
}